package com.lk.ii.controller;

import java.util.List;

import com.lk.framework.util.ShiroUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.lk.common.annotation.Log;
import com.lk.common.enums.BusinessType;
import com.lk.ii.domain.IiAuthor;
import com.lk.ii.service.IIiAuthorService;
import com.lk.common.core.controller.BaseController;
import com.lk.common.core.domain.AjaxResult;
import com.lk.common.utils.poi.ExcelUtil;
import com.lk.common.core.page.TableDataInfo;

/**
 * 作者Controller
 * 
 * @author lk
 * @date 2021-02-02
 */
@Controller
@RequestMapping("/ii/author")
public class IiAuthorController extends BaseController
{
    private String prefix = "ii/author";

    @Autowired
    private IIiAuthorService iiAuthorService;

    @RequiresPermissions("ii:author:view")
    @GetMapping()
    public String author()
    {
        return prefix + "/author";
    }

    /**
     * 查询作者列表
     */
    @RequiresPermissions("ii:author:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(IiAuthor iiAuthor)
    {
        startPage();
        List<IiAuthor> list = iiAuthorService.selectIiAuthorList(iiAuthor);
        return getDataTable(list);
    }

    /**
     * 导出作者列表
     */
    @RequiresPermissions("ii:author:export")
    @Log(title = "作者", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(IiAuthor iiAuthor)
    {
        List<IiAuthor> list = iiAuthorService.selectIiAuthorList(iiAuthor);
        ExcelUtil<IiAuthor> util = new ExcelUtil<IiAuthor>(IiAuthor.class);
        return util.exportExcel(list, "author");
    }

    /**
     * 新增作者
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存作者
     */
    @RequiresPermissions("ii:author:add")
    @Log(title = "作者", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(IiAuthor iiAuthor)
    {
        iiAuthor.setCreateBy(ShiroUtils.getLoginName());
        return toAjax(iiAuthorService.insertIiAuthor(iiAuthor));
    }

    /**
     * 修改作者
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        IiAuthor iiAuthor = iiAuthorService.selectIiAuthorById(id);
        mmap.put("iiAuthor", iiAuthor);
        return prefix + "/edit";
    }

    /**
     * 修改保存作者
     */
    @RequiresPermissions("ii:author:edit")
    @Log(title = "作者", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(IiAuthor iiAuthor)
    {
        iiAuthor.setUpdateBy(ShiroUtils.getLoginName());
        return toAjax(iiAuthorService.updateIiAuthor(iiAuthor));
    }

    /**
     * 删除作者
     */
    @RequiresPermissions("ii:author:remove")
    @Log(title = "作者", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(iiAuthorService.deleteIiAuthorByIds(ids));
    }
}
